package instant.mail;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

/**
 * @author qiangli
 * 
 */
public class EmailActivity extends Activity {

	private static final String TAG = "EmailActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//
		startEmailActivity();
	}

	private void startEmailActivity() {
		try {
			Intent my = getIntent();
			if (Constants.DEBUG_ENABLED) {
				Log.d(TAG, "startEmailActivity: " + my);
			}
			String subj = Preferences.getGreetingSubject(this, "");
			String msg = Preferences.getGreetingBody(this, "");
			String name = my.getStringExtra(EtalkIntent.EXTRA_NAME);
			String to = my.getStringExtra(EtalkIntent.EXTRA_EMAIL);
			MailTemplate.startSendActivity(this, name, to, subj, msg, null, null);
			//
			setResult(RESULT_OK);
		} catch (ActivityNotFoundException e) {
			toast(R.string.msg_noapp, e.getMessage(), Toast.LENGTH_LONG);
			setResult(RESULT_CANCELED);
		} catch (Exception e) {
			toast(R.string.msg_fail, e.getMessage(), Toast.LENGTH_LONG);
			setResult(RESULT_CANCELED);
		}
		finish();
	}

	private void toast(int resId, String arg, int duration) {
		CharSequence text = getString(resId, arg);
		Toast.makeText(this, text, duration).show();
	}
}
